package com.hdyg.sixg.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.hdyg.sixg.R;
import com.hdyg.sixg.entry.ImageBean;
import com.hdyg.sixg.util.ImageLoadUtil;
import com.hdyg.sixg.util.LogUtils;
import java.util.List;

/**
 * Created by Administrator on 2018/1/24.
 * 发布图片 - 适配器
 */
public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<ImageBean> imageBeanList;
    private OnSelectImgClickListener mImgSelectClickListener;
    private OnDelImgClickListener mImgDelClickListener;
    public final static int IMAGE_MAX_NUMBER = 9;   //最大图片张数
    public final static int IMAGE_MAX_SIZE = 1;     //限制图片张数

    public static final int TYPE_TYPE2 = 0xff03;
    public static final int TYPE_TYPE3 = 0xff04;

    public ImageAdapter(Context mContext, List imageBeanList) {
        this.mContext = mContext;
        this.imageBeanList = imageBeanList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_TYPE2:
                return new HolderType2(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_take_photo, parent, false));
            case TYPE_TYPE3:
                return new HolderType3(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_take_photo, parent, false));
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HolderType2){
            bindType2((HolderType2) holder, position);
        }else if (holder instanceof HolderType3){
            bindType3((HolderType3) holder, position);
        }

    }

    @Override
    public int getItemCount() {
        if (imageBeanList == null || imageBeanList.size() == 0){
            return 1;
        }else {
            return imageBeanList.size()+1;
        }
    }

    @Override
    public int getItemViewType(int position) {
        //根据返回的position 来绑定布局
        if (imageBeanList != null && imageBeanList.size() != 0){
            if (position == imageBeanList.size()){
                return TYPE_TYPE3;
            }else {
                return TYPE_TYPE2;
            }
        }else {
            return TYPE_TYPE3;
        }
    }

    @Override
    public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    switch (type) {
                        case TYPE_TYPE2://  3是3行2列   1是1行6列  6是6行
                            return 2;
                        case TYPE_TYPE3://  3是3行2列   1是1行6列  6是6行
                            return 2;
                        default:
                            return 1;
                    }
                }
            });
        }
    }

    /////////////////////////////
    //设置数据
    private void bindType2(final HolderType2 holder, final int position) {
        holder.ivDelete.setVisibility(View.VISIBLE);
        ImageLoadUtil.loadRoundImage(mContext, imageBeanList.get(position).getSrc(),holder.ivImage);
        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImgDelClickListener.onImgDelClick(position);
            }
        });
    }
    private void bindType3(final HolderType3 holder, final int position) {

        holder.ivDelete.setVisibility(View.GONE);
        holder.ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImgSelectClickListener.onImgSelectClick();
            }
        });
    }

    //删除图片
    public void removeData(int pos){
        imageBeanList.remove(pos);
        notifyDataSetChanged();
    }
    //添加图片
    public void addData(ImageBean imageBean){
        imageBeanList.add(imageBean);
        notifyDataSetChanged();
    }

    //返回图片张数
    public int getImageDatas(){
        return imageBeanList.size();
    }

    //返回图片集合
    public List<ImageBean> getDatas(){
        return imageBeanList;
    }


    /////////////////////////////
    //绑定数据
    public class HolderType2 extends RecyclerView.ViewHolder {
        public ImageView ivDelete;
        public ImageView ivImage;

        public HolderType2(View itemView) {
            super(itemView);
            ivDelete = (ImageView) itemView.findViewById(R.id.ivDelete);
            ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
        }
    }
    public class HolderType3 extends RecyclerView.ViewHolder {
        public ImageView ivDelete;
        public ImageView ivImage;
        public RelativeLayout rlItem;

        public HolderType3(View itemView) {
            super(itemView);
            ivDelete = (ImageView) itemView.findViewById(R.id.ivDelete);
            ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
            rlItem = (RelativeLayout) itemView.findViewById(R.id.rlItem);
        }
    }

    //点击事件
    public void setOnImgSelectClickListener(OnSelectImgClickListener listener) {
        this.mImgSelectClickListener = listener;
    }
    public void setOnImgDelClickListener(OnDelImgClickListener listener) {
        this.mImgDelClickListener = listener;
    }

    /**
     * 选择图片
     */
    public interface OnSelectImgClickListener {
        void onImgSelectClick();
    }
    //删除图片
    public interface OnDelImgClickListener {
        void onImgDelClick(int pos);
    }

}
